package services;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Date;
import java.sql.Types;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import oracle.jdbc.pool.OracleDataSource;

import entities.FilmEntity;
import entities.RezyserEntity;

/**
 * Session Bean implementation class Film
 */
@Stateless
@LocalBean
public class FilmService implements FilmRemote {
	@PersistenceContext(name = "WebFilmPU") private EntityManager em;
    public FilmService() {
        
    }

    public void addFilm(FilmEntity film)
    {
    	em.persist(film);
    }
    
    public void addFilm(String tytul, String rok, String rezyser)
    {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date utilDate;
		try {
			utilDate = df.parse(rok);
			Date data = new Date(utilDate.getTime());
			
			Query q = em.createNamedQuery("RezyserEntity.findByNazwisko");
			q.setParameter("nazwisko", rezyser);
			RezyserEntity r = (RezyserEntity)q.getSingleResult();
			
			Integer id = getMaxId()+1;
			FilmEntity f = new FilmEntity(id,tytul,data);
			f.setRezyserBean(r);
			
			addFilm(f);
			
		} catch (ParseException e) {
			e.printStackTrace();
		}
    }
    
	@SuppressWarnings("unchecked")
	public List<FilmEntity> getAll()
    {
		List<FilmEntity> lista = em.createNamedQuery("FilmEntity.findAll").getResultList();
    	
		return lista;
    }
    
	public FilmEntity getFilm(Integer id)
	{
		FilmEntity f = em.find(FilmEntity.class, id);
		return f;
	}
	
    private Integer getMaxId() {
		 
		Query maxIdQuery = em.createQuery("SELECT max(r.id) FROM FilmEntity r");
 
		Integer maxId = 1;
 
		if( (maxIdQuery.getResultList() != null) && (maxIdQuery.getResultList().size() > 0) ) {
 
			maxId = (Integer)maxIdQuery.getResultList().get(0);
 
		}
 
		return maxId;
	}
    
    public void removeFilm(Integer id)
	{
		FilmEntity f = getFilm(id);
		if(f!=null)
		{
			em.remove(f);
		}
	}
	
	public void updateFilm(Integer id, String tytul, String rok, String nazwisko)
	{
		try {
			FilmEntity f = getFilm(id);
			f.setTytul(tytul);
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
			Date d = new Date(sf.parse(rok).getTime());
			f.setRok(d);
			Query q = em.createNamedQuery("RezyserEntity.findByNazwisko");
			q.setParameter("nazwisko", nazwisko);
			RezyserEntity r = (RezyserEntity)q.getSingleResult();
			f.setRezyserBean(r);
			
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
	
	public void test() throws Exception
	{
		OracleDataSource ods = new OracleDataSource();
		ods.setURL("jdbc:oracle:thin:@<hoststring>");
		ods.setUser("hr");
		ods.setPassword("hr");
		
		Connection conn = ods.getConnection();
		CallableStatement cs = conn.prepareCall ("begin ? := foo(?); end;");
		cs.registerOutParameter(1,Types.CHAR);
		cs.setString(2, "aa");
		cs.executeUpdate();
		String result = cs.getString(1);
		
		
	}
}
